# 测试房间API功能
Write-Host "🧪 测试房间API功能" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan

# 等待后端启动
Write-Host "⏳ 等待后端启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 5

# 测试创建房间
Write-Host "`n🏠 测试创建房间..." -ForegroundColor Green
$createRoomBody = @{
    name = "测试房间"
    password = $null
    maxPlayers = 4
    gameDuration = 180
    playerNickName = "测试玩家"
} | ConvertTo-Json

try {
    $createResponse = Invoke-RestMethod -Uri "http://localhost:5000/api/commands/create-room" -Method POST -Body $createRoomBody -ContentType "application/json"
    Write-Host "✅ 房间创建成功!" -ForegroundColor Green
    Write-Host "   房间代码: $($createResponse.roomCode)" -ForegroundColor White
    Write-Host "   房间ID: $($createResponse.roomId)" -ForegroundColor White
    
    $roomCode = $createResponse.roomCode
    
    # 测试通过房间代码查询房间
    Write-Host "`n🔍 测试通过房间代码查询房间..." -ForegroundColor Green
    $roomResponse = Invoke-RestMethod -Uri "http://localhost:5000/api/queries/rooms/$roomCode" -Method GET
    Write-Host "✅ 房间查询成功!" -ForegroundColor Green
    Write-Host "   房间名称: $($roomResponse.name)" -ForegroundColor White
    Write-Host "   房间状态: $($roomResponse.status)" -ForegroundColor White
    Write-Host "   当前玩家数: $($roomResponse.currentPlayers)" -ForegroundColor White
    
    # 测试通过GUID查询房间
    Write-Host "`n🔍 测试通过GUID查询房间..." -ForegroundColor Green
    $roomResponse2 = Invoke-RestMethod -Uri "http://localhost:5000/api/queries/rooms/$($createResponse.roomId)" -Method GET
    Write-Host "✅ GUID查询成功!" -ForegroundColor Green
    Write-Host "   房间名称: $($roomResponse2.name)" -ForegroundColor White
    
    # 测试获取所有房间
    Write-Host "`n📋 测试获取所有房间..." -ForegroundColor Green
    $roomsResponse = Invoke-RestMethod -Uri "http://localhost:5000/api/queries/rooms" -Method GET
    Write-Host "✅ 获取房间列表成功!" -ForegroundColor Green
    Write-Host "   房间总数: $($roomsResponse.rooms.Count)" -ForegroundColor White
    
} catch {
    Write-Host "❌ 测试失败: $($_.Exception.Message)" -ForegroundColor Red
    if ($_.Exception.Response) {
        $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
        $responseBody = $reader.ReadToEnd()
        Write-Host "   响应内容: $responseBody" -ForegroundColor Red
    }
}

Write-Host "`n🎯 测试完成!" -ForegroundColor Cyan
